package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPacksLeagalBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.sgb.kjwl.model.entity.packers.PackersLegalEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.packers.PackersLegalAdapter;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.viewmodel.packers.PackersLegalModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * description:法务团队
 * @author 毁灭霸者
 * @date 2020/6/30
 */
@Deprecated
public class PackersLegalActivity extends AppActivity {

    @Inject
    PackersLegalModel viewModel;
    private ActivityPacksLeagalBinding binding;
    private PackersLegalAdapter packersLegalAdapter;

    private DictWindow5 typeWindow5;
    private String typeId = "";
    private List<DictEntity3> typeDictEntityList;
    private DictWindow5 areaWindow5;
    private String areaId = "";
    private List<DictEntity3> areaDictEntityList;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPackersComponent.builder().appComponent(appComponent).packersModule(new PackersModule(this)).build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, PackersLegalActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_packs_leagal);
        binding.setViewModel(viewModel);
        binding.typeLayout.setOnClickListener(this);
        binding.areaLayout.setOnClickListener(this);

        if (packersLegalAdapter == null) {
            packersLegalAdapter = new PackersLegalAdapter(getActivity(), null);
        }
        packersLegalAdapter.setOnItemClick(new AdapterOnItemClick<PackersLegalEntity>() {
            @Override
            public void onItemClick(PackersLegalEntity packersLegalEntity, int position) {
                viewModel.getH5Url(packersLegalEntity.getId(), packersLegalEntity.getNo());
            }
        });
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp10), ContextCompat.getColor(this,R.color.color_F5F5F5)));

        binding.recyclerView.setAdapter(packersLegalAdapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getLegalListModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getLegalListModel(true);
            }
        });

        initFilterView();
    }

    private void initFilterView() {
        typeWindow5 = new DictWindow5(getActivity());
        typeWindow5.setFocusable(false);
        typeWindow5.setOutsideTouchable(false);
        typeWindow5.setOnCallBack(dictEntity -> {
            typeId = dictEntity.getId();
            if (!"-1".equals(typeId) && !"".equals(typeId)) {
                binding.typeTv.setText(dictEntity.getName());
                binding.typeTv.setTextColor(ContextCompat.getColor(this,R.color.color_main));
                viewModel.map.get().put("firstCategoryNo", typeId);
            } else {
                binding.typeTv.setText("法务类型");
                binding.typeTv.setTextColor(ContextCompat.getColor(this,R.color.color_333333));
                viewModel.map.get().put("firstCategoryNo", null);
            }
            viewModel.getLegalListModel(false);
        });
        typeWindow5.setOnCloseBack(() -> binding.typeArrowView.setImageResource(R.mipmap.ic_down_arrow_n));

        areaWindow5 = new DictWindow5(getActivity());
        areaWindow5.setFocusable(false);
        areaWindow5.setOutsideTouchable(false);
        areaWindow5.setOnCallBack(dictEntity -> {
            areaId = dictEntity.getId();
            if (!"-1".equals(areaId)) {
                binding.areaTv.setText(dictEntity.getName());
                binding.areaTv.setTextColor(ContextCompat.getColor(PackersLegalActivity.this,R.color.color_main));
                viewModel.map.get().put("cityNo", areaId);
            } else {
                binding.areaTv.setText("所在地区");
                binding.areaTv.setTextColor(ContextCompat.getColor(PackersLegalActivity.this,R.color.color_333333));
                viewModel.map.get().put("cityNo", null);
            }
            viewModel.getLegalListModel(false);
        });
        areaWindow5.setOnCloseBack(() -> binding.areaArrowView.setImageResource(R.mipmap.ic_down_arrow_n));
    }

    @Override
    protected void init() {
        viewModel.getLegalListModel(false);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.type_layout) {
            if (areaWindow5.isShowing()) {
                areaWindow5.dismiss();
            }
            binding.typeArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
            typeWindow5.setCheckDefaultById(typeId);
            typeWindow5.showPopwindow(binding.typeLayout);
            if (typeDictEntityList == null) {
                viewModel.getFilterType();
            }
        } else if (id == R.id.area_layout) {
            if (typeWindow5.isShowing()) {
                typeWindow5.dismiss();
            }
            binding.areaArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
            areaWindow5.setCheckDefaultById(areaId);
            areaWindow5.showPopwindow(binding.areaLayout);
            if (areaDictEntityList == null) {
                viewModel.getFilterArea();
            }
        }
    }

    public void showTypePopWindow(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        if (!getString(R.string.app_all).equals(data.get(0).getName())) {
            data.add(0, viewModel.dictEntity3.get());
        }
        this.typeDictEntityList = data;
        if (typeId == null || "".equals(typeId)) {
            typeId = data.get(0).getId();
        }
        typeWindow5.setData(data);
        typeWindow5.setCheckDefaultById(typeId);
    }

    public void showAreaPopWindow(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        if (!getString(R.string.app_all).equals(data.get(0).getName())) {
            data.add(0, viewModel.dictEntity3.get());
        }
        this.areaDictEntityList = data;
        if (areaId == null || "".equals(areaId)) {
            areaId = data.get(0).getId();
        }
        areaWindow5.setData(data);
        areaWindow5.setCheckDefaultById(areaId);
    }

    public void showList(List<PackersLegalEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                packersLegalAdapter.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            packersLegalAdapter.setDatas(list);
        }
    }
}
